#!/bin/bash

# API功能测试脚本 - 192.168.30.100串口服务器
# 测试设备：云台通道0000000000000002，IO通道d4ad2070b92f0002
# API访问：http://192.168.15.125:5000

API_BASE="http://192.168.15.125:5000"
PTZ_CHANNEL="0000000000000002"
IO_CHANNEL="d4ad2070b92f0002"
PTZ_ADDRESS="20"
IO_ADDRESS="1"

echo "========================================"
echo "🎯 API功能测试 - 192.168.30.100串口服务器"
echo "========================================"
echo ""
echo "📋 测试设备信息："
echo "- 串口服务器IP: 192.168.30.100"
echo "- 云台通道: $PTZ_CHANNEL"
echo "- IO通道: $IO_CHANNEL"
echo "- 云台地址码: $PTZ_ADDRESS"
echo "- IO地址码: $IO_ADDRESS"
echo "- API访问: $API_BASE"
echo ""

# 1. 云台功能测试
echo "🔄 1. 云台功能测试"
echo "----------------------------------------"

echo "1.1 查询当前云台角度："
PAN_ANGLE=$(curl -s "$API_BASE/test/ptz/getAngleValue?channelName=$PTZ_CHANNEL&slaveAddress=$PTZ_ADDRESS&direction=PAN")
TILT_ANGLE=$(curl -s "$API_BASE/test/ptz/getAngleValue?channelName=$PTZ_CHANNEL&slaveAddress=$PTZ_ADDRESS&direction=TILT")
echo "- PAN (水平): $PAN_ANGLE°"
echo "- TILT (垂直): $TILT_ANGLE°"
echo ""

echo "1.2 设置云台角度到0位置："
curl -s -X POST "$API_BASE/test/ptz/sendSpinCmd" \
  -H "Content-Type: application/json" \
  -d "{\"channelName\":\"$PTZ_CHANNEL\",\"address\":$PTZ_ADDRESS,\"direction\":\"PAN\",\"angle\":0}" > /dev/null
echo "- PAN设置为0°"

curl -s -X POST "$API_BASE/test/ptz/sendSpinCmd" \
  -H "Content-Type: application/json" \
  -d "{\"channelName\":\"$PTZ_CHANNEL\",\"address\":$PTZ_ADDRESS,\"direction\":\"TILT\",\"angle\":0}" > /dev/null
echo "- TILT设置为0°"
echo ""

sleep 3

echo "1.3 验证角度设置："
PAN_ANGLE=$(curl -s "$API_BASE/test/ptz/getAngleValue?channelName=$PTZ_CHANNEL&slaveAddress=$PTZ_ADDRESS&direction=PAN")
TILT_ANGLE=$(curl -s "$API_BASE/test/ptz/getAngleValue?channelName=$PTZ_CHANNEL&slaveAddress=$PTZ_ADDRESS&direction=TILT")
echo "- PAN (水平): $PAN_ANGLE°"
echo "- TILT (垂直): $TILT_ANGLE°"
echo ""

echo "1.4 测试速度设置："
curl -s -X POST "$API_BASE/test/ptz/sendSetupSpeedCmd" \
  -H "Content-Type: application/json" \
  -d "{\"channelName\":\"$PTZ_CHANNEL\",\"address\":$PTZ_ADDRESS,\"direction\":\"PAN\",\"speed\":1.0}" > /dev/null
echo "- PAN速度设置为1.0°/s"

curl -s -X POST "$API_BASE/test/ptz/sendSetupSpeedCmd" \
  -H "Content-Type: application/json" \
  -d "{\"channelName\":\"$PTZ_CHANNEL\",\"address\":$PTZ_ADDRESS,\"direction\":\"TILT\",\"speed\":1.0}" > /dev/null
echo "- TILT速度设置为1.0°/s"
echo ""

# 2. 继电器功能测试
echo "🔌 2. 继电器功能测试"
echo "----------------------------------------"

echo "2.1 查询当前继电器状态："
RELAY_STATUS=$(curl -s "$API_BASE/test/relay/list" | jq -r ".\"$IO_CHANNEL-$IO_ADDRESS\"")
echo "- 继电器状态: $RELAY_STATUS"
echo ""

echo "2.2 测试继电器开关控制："
for i in {1..6}; do
    echo "- 开启继电器 $i"
    curl -s -X POST "$API_BASE/test/relay/doControl" \
      -H "Content-Type: application/json" \
      -d "{\"channelName\":\"$IO_CHANNEL\",\"address\":$IO_ADDRESS,\"doNumber\":$i,\"f\":true}" > /dev/null
    sleep 1
done
echo ""

echo "2.3 验证继电器状态："
RELAY_STATUS=$(curl -s "$API_BASE/test/relay/list" | jq -r ".\"$IO_CHANNEL-$IO_ADDRESS\"")
echo "- 继电器状态: $RELAY_STATUS (应该全部为1)"
echo ""

echo "2.4 关闭所有继电器："
for i in {1..6}; do
    echo "- 关闭继电器 $i"
    curl -s -X POST "$API_BASE/test/relay/doControl" \
      -H "Content-Type: application/json" \
      -d "{\"channelName\":\"$IO_CHANNEL\",\"address\":$IO_ADDRESS,\"doNumber\":$i,\"f\":false}" > /dev/null
    sleep 1
done
echo ""

echo "2.5 验证继电器关闭："
RELAY_STATUS=$(curl -s "$API_BASE/test/relay/list" | jq -r ".\"$IO_CHANNEL-$IO_ADDRESS\"")
echo "- 继电器状态: $RELAY_STATUS (应该全部为0)"
echo ""

# 3. 系统状态查询测试
echo "📊 3. 系统状态查询测试"
echo "----------------------------------------"

echo "3.1 查询所有云台角度："
curl -s "$API_BASE/test/ptz/list" | jq .
echo ""

echo "3.2 查询所有继电器状态："
curl -s "$API_BASE/test/relay/list" | jq .
echo ""

echo "3.3 查询通道连接状态："
curl -s "$API_BASE/channel/list" | jq .
echo ""

echo "========================================"
echo "✅ API功能测试完成"
echo "========================================"
